<template>
  <div>
    <el-collapse-transition>
      <card-search v-if="searchCardShow">
        <el-form ref="form" :model="form" label-width="80px" :inline="true">
          <el-form-item label="登录名称">
            <el-input></el-input>
          </el-form-item>
          <el-form-item label="用户状态">
            <el-select
              v-model="value"
              placeholder="请选择"
              style="width: 250px"
              clearable
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="value"
              style="width: 250px"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
          <el-form-item>
            <el-button class="btn-search" round icon="el-icon-search" size="medium"
              >搜索</el-button
            >
            <el-button class="btn-search" round icon="el-icon-refresh" size="medium"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </card-search>
    </el-collapse-transition>
    <card-table style="height: calc(100vh - 100px)">
      <div class="card-table-header">
        <div class="card-table-header-btns">
          <el-button
            class="btn-search"
            size="small"
            icon="el-icon-download"
            v-if="isSuperAdmin || hasPermission('system:user:import')"
            >导入</el-button
          >
          <el-button
            class="btn-search"
            size="small"
            icon="el-icon-upload2"
            v-if="isSuperAdmin || hasPermission('system:user:export')"
            >导出</el-button
          >

          <el-input
            v-if="isSuperAdmin || hasPermission('system:user:list')"
            v-model="input"
            placeholder="请输入手机号"
            style="width: 450px; margin-left: 10px"
            clearable
            size="small"
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </div>
        <div class="card-table-header-actions">
          <table-action
            :search-show="isSuperAdmin || hasPermission('system:user:list')"
            :table-header="tableHeader"
            :eye-show="false"
            @check-change="handleCheckChange"
            @search-click="handleSearchClick"
            @init-click="handleInitClick"
          />
        </div>
      </div>
      <el-table
        :key="tableKey"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        :header-cell-style="{ background: '#eff3f8', color: '#333' }"
        size="small"
        stripe
        :default-sort="{ prop: 'date', order: 'descending' }"
        :max-height="tableHeight"
        @selection-change="handleSelectionChange"
        highlight-selection-row
        border
      >
        <el-table-column type="index" width="55" align="center" />
        <el-table-column
          v-for="item in tableHeader"
          v-if="item.visible"
          :key="item.prop"
          :label="item.title"
          :min-width="item.width"
          :prop="item.prop"
          :show-overflow-tooltip="true"
          :align="item.align"
          sortable
        >
          <template #default="scope">
            <span v-if="item.prop === 'status'" class="link-type">
              <el-switch v-model="scope.row[item.prop]" size="small" :width="35"
            /></span>
            <span v-else>
              {{ scope.row[item.prop] }}
            </span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="isSuperAdmin || hasPermission('system:user:remove')"
              type="primary"
              size="mini"
              icon="el-icon-delete"
              @click="handleClick(null, 'delete')"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <XyPagination
        :total="total"
        :pageSizeNum="pageSize"
        :pageNum="currentPage"
        @pagination="handlerPagination"
      ></XyPagination>
    </card-table>

    <ManagerSet
      v-model="dialogChangeVisible"
      :field="field"
      @close="handleDialogChangeClose"
    />
    <UserInfo
      v-model="dialogDetailVisible"
      :field="field"
      @close="handleDialogDetailClose"
    />
  </div>
</template>
<script>
import CardSearch from "@/components/CardSearch";
import CardTable from "@/components/CardTable";
import TableAction from "@/components/TableAction";
import ManagerSet from "../components/DialogManagerset.vue";
import UserInfo from "../components/DialogUserinfo.vue";
import XyPagination from "@/components/XyPagination";
import { hasPermission, isSuperAdmin } from "@/utils/hasBtnPermission";
export default {
  components: {
    CardSearch,
    CardTable,
    ManagerSet,
    TableAction,
    UserInfo,
    XyPagination,
  },
  data() {
    return {
      tableKey: 0, // 用于强制刷新表格
      searchCardShow: false,
      input: "",
      tableHeader: [
        {
          title: "登录名称",
          prop: "projectName",
          width: "150",
          visible: true,
          align: "left",
        },
        {
          title: "用户姓名",
          prop: "projectAdmin",
          width: "120",
          visible: true,
          align: "left",
        },
        {
          title: "所属企业",
          prop: "company",
          width: "150",
          visible: true,
          align: "left",
        },
        {
          title: "手机",
          prop: "totleNum",
          width: "150",
          visible: true,
          align: "center",
        },
        {
          title: "用户状态",
          prop: "status",
          width: "80",
          visible: true,
          align: "center",
        },
        {
          title: "到期时间",
          prop: "updateTime",
          width: "120",
          visible: true,
          align: "center",
        },
        {
          title: "创建时间",
          prop: "date",
          width: "150",
          visible: true,
          align: "center",
        },
        {
          title: "备注",
          prop: "mark",
          width: "120",
          visible: true,
          align: "center",
        },
      ],
      tableData: [
        {
          date: "2016-05-03",
          updateTime: "2016-05-03",
          name: "王小虎",
          projectName: "久诺新能源",
          projectAdmin: "13204053226",
          company: "鑫源直属项目管理平台",
          totleNum: "13654079988",
          status: true,
          personInCharge: "普通用户",
          address: "上海市普陀区金沙江路 1518 弄",
        },
      ],
      multipleSelection: [],
      btnDisabled: true,
      currentPage: 1,
      pageSize: 20,
      total: 0,
      dialogDetailVisible: false,
      dialogChangeVisible: false,
      field: "",
      tableHeight: 550,
      value: "",
      options: [
        { label: "所有", value: "" },
        { label: "正常", value: "1" },
        { label: "停用", value: "2" },
      ],
    };
  },
  computed: {
    isSuperAdmin() {
      return isSuperAdmin();
    },
  },
  mounted() {
    this.calculateTableHeight();
    this.initTable();
  },
  methods: {
    initTable() {},
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      if (this.multipleSelection.length > 0) {
        this.btnDisabled = false;
      } else {
        this.btnDisabled = true;
      }
      console.log(this.multipleSelection);
    },
    handleClick(row, field) {
      switch (field) {
        case "delete":
          this.$confirm(`确认删除当前企业管理员吗?`, "系统提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
          break;
        default:
          break;
      }
    },
    // 处理表头显示隐藏
    handleCheckChange(checkedValues) {
      this.tableHeader.forEach((item) => {
        item.visible = checkedValues.includes(item.prop);
      });
      this.tableKey += 1;
    },
    handleSearchClick() {
      this.searchCardShow = !this.searchCardShow;
      console.log("搜索按钮点击");
    },
    handleInitClick() {
      this.initTable();
    },
    handleDialogDetailClose() {
      this.field = "";
      this.dialogDetailVisible = false;
    },
    handleDialogChangeClose() {
      this.dialogChangeVisible = false;
    },
    handlerPagination(page) {
      this.currentPage = page.pageNum;
      this.pageSize = page.pageSize;
      this.initTable();
    },
    hasPermission(permission) {
      return hasPermission(permission);
    },
    calculateTableHeight() {
      // 根据容器实际高度计算表格高度
      this.$nextTick(() => {
        const container = this.$refs.multipleTable.$el?.parentElement;
        if (container) {
          // 减去表头和其他元素的高度
          this.tableHeight = container.clientHeight - 120; // 120为估计的其他元素高度
        }
      });
    },
  },
};
</script>
